<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>hello react</title>
    <style></style>
  </head>
  <body>
    <div id="test"></div>
    <!-- 引入react核心库 -->
    <script src="../js/react.development.js"></script>
    <!-- 引入react Dom库 用于支持 react操作dom-->
    <script src="../js/react-dom.development.js"></script>
    <!-- 引入babel 用于将 jsx 转为 js -->
    <script src="../js/babel.min.js"></script>

    <!-- 使用babel 转译jsx -->
    <script type="text/babel">
      // 1.创建组件
      class Login extends React.Component {
        state = {
          username: "",
          password: "",
        };
        saveFormData = (dataType, e) => {
          this.setState({
            [dataType]: e.target.value,
          });
        };
        handleSubmit = (e) => {
          // 阻止表单提交
          e.preventDefault();
          const { username, password } = this.state;
          alert(`用户名：${username}` + "  " + `密码：${password}`);
        };
        render() {
          return (
            <form onSubmit={this.handleSubmit}>
              用户名：
              <input
                onChange={(event) => {
                  this.saveFormData("username", event);
                }}
                type="text"
                name="username"
              />
              密码：
              <input
                onChange={(event) => {
                  this.saveFormData("password", event);
                }}
                type="password"
                name="password"
              />
              <button>登陆</button>
            </form>
          );
        }
      }
      // 2.渲染虚拟DOM到页面
      ReactDOM.render(<Login />, document.getElementById("test"));
    </script>
  </body>
</html>
